package GameWorld;

import java.util.Random;
import javax.swing.ImageIcon;
import java.awt.Graphics;
public abstract class SeaObject {

    public static final int LIVE = 0;
    public static final int DEAD = 1; //死了
    protected int state = LIVE;
    protected int width,height,x,y,speed;


    SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT-height-150+1)+150;
        speed = rand.nextInt(3)+1;
    }

    SeaObject(int width,int height,int x,int y,int speed){
            this.width = width;
            this.height = height;
            this.x = x;
            this.y = y;
            this.speed = speed;
    }

    public abstract void move();
    public abstract ImageIcon getImage();

    public boolean isLive(){
        return state==LIVE;
    }//判断对象是否活着
    public boolean isDead(){
        return state==DEAD;
    }

    public void painImage(Graphics g){
        if (this.isLive()){
            this.getImage().paintIcon(null,g,this.x,this.y);
            System.out.println("ssffd");
        }
    }




    public boolean isOutofBounds(){
            return this.x >= World.WIDTH;//潜艇的x>=窗口的宽   即为越界
    }
    public boolean isHit(SeaObject other) {
        int x1 = this.x - other.width;
        int y1 = this.y - other.height;
        int x2 = this.x + this.width;
        int y2 = this.y + this.height;
        int x = other.x;
        int y = other.y;
        return x >= x1 && x <= x2 && y >= y1 && y <= y2;
    }
    public void goDead(){
        state = DEAD;//将对象状态设置为死的DEAD
    }

}
